package indi.woniu.security.handler;


import indi.woniu.entity.ResponseEntity;
import indi.woniu.field.StatusCode;
import indi.woniu.utils.Util;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: GaN601
 * @Date: 2021/12/8 16:02
 * @Description:
 */
@Configuration
public class AuthenticationFailHandler implements AuthenticationFailureHandler {
    
    @Override
    public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response,
            final AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        
        response.getWriter().write(
                Util.toJsonStr(new ResponseEntity<>(StatusCode.ERR_USER, "认证失败", exception.getLocalizedMessage())));
    }
}
